package com.zysc.processflow.common.util;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.zysc.processflow.common.base.BaseController;
import com.zysc.processflow.common.base.BaseEntity;
import com.zysc.processflow.common.base.BaseService;

import java.util.Collections;
import java.util.Scanner;

public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        String[] arr = new String[]{"id", "creatorid", "creator", "createtime", "updaterid", "updater", "updatetime"};// 写于父类中的公共字段

        FastAutoGenerator.create("jdbc:postgresql://192.168.1.176:5432/processflow?useSSL=false", "postgres", "zy123456")
                // 数据源设置
                .dataSourceConfig((builder) -> builder.schema("public"))
                // 全局配置
                .globalConfig((builder) -> builder.author("系统集成")
                        .disableOpenDir()
                        .dateType(DateType.TIME_PACK)
                        .outputDir(projectPath + "/src/main/java")
                        .commentDate("yyyy-MM-dd").build())

                // 包配置.pathInfo(
                .packageConfig((scanner, builder) -> builder
                        .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper"))
//                        .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/iot/mapper"))
                        .parent(scanner.apply("请输入包名")))
                // 策略配置
                .strategyConfig((scanner, builder) -> builder.addInclude(scanner.apply("请输入表名，多个表名用,隔开"))
                        .entityBuilder().superClass(BaseEntity.class).addSuperEntityColumns(arr)
                        .columnNaming(NamingStrategy.no_change)//字段下滑线后首字母转大写，这里不转，数据库里为准
                        .naming(NamingStrategy.underline_to_camel)//表名称下滑线后首字母转大写
                        .serviceBuilder().superServiceImplClass(BaseService.class).formatServiceFileName("%sService")
                        .formatServiceImplFileName("%sServiceImpl")
                        .controllerBuilder().superClass(BaseController.class).enableHyphenStyle().enableRestStyle()
                )
                /* 模板引擎配置，默认 Velocity 可选模板引擎 Beetl 或 Freemarker 或 Enjoy
                 .templateEngine(new BeetlTemplateEngine())
                 .templateEngine(new FreemarkerTemplateEngine())
                 .templateEngine(new EnjoyTemplateEngine())*/
                // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }}
